package com.health.httpbean.upload;

import android.os.AsyncTask;
import android.text.TextUtils;
import android.util.Log;

import com.blankj.utilcode.util.GsonUtils;
import com.blankj.utilcode.util.LogUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.blankj.utilcode.util.UiMessageUtils;
import com.blankj.utilcode.util.Utils;
import com.google.gson.Gson;
import com.health.httpbean.HttpConstant;
import com.health.httpbean.HttpDataBean;
import com.health.httpbean.HttpResultBean;
import com.health.httpbean.QueryTypeEnum;
import com.health.httpbean.format.FormatFjhData;
import com.health.tablebean.BD_DD_INFO;
import com.health.tablebean.CD_TUBERCULOSIS_FOLLOWUP;
import com.health.tablebean.SBCL_DATA;
import com.health.tablebean.webservice.MeasurementDataNew;
import com.health.tools.GlobalConts;
import com.health.viewscontrols.NovaApplication;
import com.health.viewscontrols.settings.DataDownLoadMainActivity;

import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;

import java.util.List;

import rxhttp.RxHttp;

/**
 * author : xiaoxin
 * date   : 2020/12/19 19:02
 * desc   : 体检数据上传到老平台
 * update :
 */
public class TjDataUpload {

    String result;

    public void uploadData() {
        if (!GlobalConts.isNetworkAvailable(Utils.getApp())) {
            // showTextDialog("请先检查网络");
            return;
        }

        List<MeasurementDataNew> dataList = MeasurementDataNew.converData().getDataList();
        if (dataList.size() > 0) {
            updata_SBCL();
        } else {
//            showTextDialog("无数据可上传");
        }
    }

    public String getRemoteInfo() throws Exception{
        String WSDL_URI = GlobalConts.getUrlPost()+"webservice/UploadService/addEquipment";//wsdl 的uri
        String namespace = "http://webservice.yide.com/";//namespace
        String methodName = "addEquipment";//要调用的方法名称
        List<MeasurementDataNew> dataList = MeasurementDataNew.converData().getDataList();
        Gson gson = new Gson();
        SoapObject request = new SoapObject(namespace, methodName);
        // 设置需调用WebService接口需要传入的两个参数mobileCode、userId
        String json=gson.toJson(dataList);
        Log.e("***测量数据",json);
        request.addProperty("EquipmentJson", json);


        //创建SoapSerializationEnvelope 对象，同时指定soap版本号(之前在wsdl中看到的)
        SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapSerializationEnvelope.VER10);
        envelope.bodyOut = request;//由于是发送请求，所以是设置bodyOut
//        envelope.dotNet = true;//由于是.net开发的webservice，所以这里要设置为true

        HttpTransportSE httpTransportSE = new HttpTransportSE(WSDL_URI);
        httpTransportSE.call(null, envelope);//调用

        // 获取返回的数据
        SoapObject object = (SoapObject) envelope.bodyIn;
        // 获取返回的结果
        result = object.getProperty(0).toString();
        Log.d("debug",result);
        return result;

    }

    public void updata_SBCL(){
//        showProgressDialog("正在上传，请稍候");
        //启动后台异步线程进行连接webService操作，并且根据返回结果在主线程中改变UI
        QueryAddressTask queryAddressTask = new QueryAddressTask();
        //启动后台任务
        queryAddressTask.execute();
    }

    class QueryAddressTask extends AsyncTask<String, Integer, String> {
        @Override
        protected String doInBackground(String... params) {
            // 查询手机号码（段）信息*/
            try {
                result = getRemoteInfo();

            } catch (Exception e) {
                e.printStackTrace();
            }
            //将结果返回给onPostExecute方法
            return result;
        }

        @Override
        //此方法可以在主线程改变UI
        protected void onPostExecute(String result) {
            // 将WebService返回的结果显示在TextView中
            if(!TextUtils.isEmpty(result)){
//                showTextDialog("上传完成");
                List<SBCL_DATA> list = NovaApplication.fdbmanger.findAllByWhere(SBCL_DATA.class,"IS_UPDATE is not 'N'" ,
                        "CL_TIME desc");
                if(list.size()>0){
                    for(SBCL_DATA sbcl_data:list){
                        sbcl_data.setIS_UPDATE("N");
                        NovaApplication.fdbmanger.updata(sbcl_data);
                    }
                }

//                cancelProgressDialog();
                Log.e("*********",result);
            }else{
//                showTextDialog("网络不给力");
//                cancelProgressDialog();
            }

        }
    }

}
